/*
 * Copyright (C) 2016 Jared Rummler
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xui_lib.component.spinner;

import com.xuexiang.xui_lib.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.configuration.Configuration;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.List;

public abstract class MaterialSpinnerBaseAdapter<T> extends RecycleItemProvider {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MaterialSpinnerBaseAdapter");

    private final Context context;
    private int selectedIndex;
    private int textColor;
    private int textSize;
    private StateElement backgroundSelector;
    private int popupPaddingTop;
    private int popupPaddingLeft;
    private int popupPaddingBottom;
    private int popupPaddingRight;
    private boolean isHintEnabled;

    public MaterialSpinnerBaseAdapter(Context context) {
        this.context = context;
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer parent) {
        final Text textView;
        if (convertView == null) {
            LayoutScatter inflater = LayoutScatter.getInstance(context);
            convertView = inflater.parse(ResourceTable.Layout_ms__list_item, parent, false);
            textView = (Text) convertView.findComponentById(ResourceTable.Id_tv_tinted_spinner);
            textView.setTextColor(new Color(textColor));
            textView.setTextSize(textSize);

            textView.setTruncationMode(Text.TruncationMode.AUTO_SCROLLING);
            textView.setAutoScrollingCount(Text.AUTO_SCROLLING_FOREVER);
            textView.startAutoScrolling();

            textView.setPaddingLeft(popupPaddingLeft + textView.getPaddingLeft());
            textView.setPaddingTop(popupPaddingTop + textView.getPaddingTop());
            textView.setPaddingRight(popupPaddingRight + textView.getPaddingRight());
            textView.setPaddingBottom(popupPaddingBottom + textView.getPaddingBottom());

            int[] stateSet0 = backgroundSelector.getStateSet(0);
            int[] stateSet1 = backgroundSelector.getStateSet(1);

            ShapeElement shapeElement0 = (ShapeElement) backgroundSelector.getStateElement(0);
            ShapeElement shapeElement1 = (ShapeElement) backgroundSelector.getStateElement(1);

            StateElement stateElement = new StateElement();
            stateElement.addState(stateSet0, shapeElement0);
            stateElement.addState(stateSet1, shapeElement1);

            textView.setBackground(stateElement);

            Configuration config = context.getResourceManager().getConfiguration();
            if (config.isLayoutRTL) {
                textView.setLayoutDirection(Component.LayoutDirection.RTL);
            }
            convertView.setTag(new ViewHolder(textView));
        } else {
            textView = ((ViewHolder) convertView.getTag()).textView;
        }
        textView.setText(getItemText(position));
        return convertView;
    }

    public String getItemText(int position) {
        return getItem(position).toString();
    }

    public int getSelectedIndex() {
        return selectedIndex;
    }

    public void notifyItemSelected(int index) {
        selectedIndex = index;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public abstract T getItem(int position);

    @Override
    public abstract int getCount();

    public abstract T get(int position);

    public abstract List<T> getItems();

    public void setHintEnabled(boolean isHintEnabled) {
        this.isHintEnabled = isHintEnabled;
    }

    public boolean isHintEnabled() {
        return this.isHintEnabled;
    }

    public MaterialSpinnerBaseAdapter<T> setTextColor(int textColor) {
        this.textColor = textColor;
        return this;
    }

    public MaterialSpinnerBaseAdapter<T> setTextSize(int textSize) {
        this.textSize = textSize;
        return this;
    }

    public MaterialSpinnerBaseAdapter<T> setBackgroundSelector(StateElement backgroundSelector) {
        this.backgroundSelector = backgroundSelector;
        return this;
    }

    public MaterialSpinnerBaseAdapter<T> setPopupPadding(int left, int top, int right, int bottom) {
        this.popupPaddingLeft = left;
        this.popupPaddingTop = top;
        this.popupPaddingRight = right;
        this.popupPaddingBottom = bottom;
        return this;
    }

    private static class ViewHolder {
        private Text textView;

        private ViewHolder(Text textView) {
            this.textView = textView;
        }
    }
}